package com.syq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syq.mapper.DtsOrderMapper;
import com.syq.pojo.DtsGoods;
import com.syq.pojo.DtsOrder;
import com.syq.pojo.DtsOrderGoods;
import com.syq.service.DtsGoodsService;
import com.syq.service.DtsOrderService;
import com.syq.util.ResponseUtil;
import com.syq.vo.DayStatis;
import com.syq.vo.OrderGoods;
import com.syq.vo.OrderStat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: DtsOrderServiceImpl
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/22 15:12
 */
@Transactional
@Service
public class DtsOrderServiceImpl implements DtsOrderService {

    @Autowired
    private DtsOrderMapper dtsOrderMapper;

    @Autowired
    private DtsGoodsService dtsGoodsService;

    @Override
    public List<DayStatis> findDayStatis() {
        return dtsOrderMapper.findDayStatis();
    }

    @Override
    public Long findCount() {
        return dtsOrderMapper.selectCount(null);
    }

    @Override
    public Object findAll(Integer page, Integer limit, String sort, String order, Integer orderSn, Integer userId, Integer[] orderStatusArray) {
        Map<String,Object> map=new HashMap<>();
        Page<DtsOrder> dtsOrderPage = dtsOrderMapper.selectPage(new Page<>(page, limit),
                new QueryWrapper<DtsOrder>()
                        .eq("deleted", false)
                        .eq(!StringUtils.isEmpty(orderSn), "order_sn", orderSn)
                        .eq(!StringUtils.isEmpty(userId), "user_id", userId)
                        .in(!StringUtils.isEmpty(orderStatusArray), "order_status", orderStatusArray)
                        .orderBy(!StringUtils.isEmpty(sort), "Asc".equalsIgnoreCase(order), sort));
        map.put("total",dtsOrderPage.getTotal());
        map.put("items",dtsOrderPage.getRecords());
        return ResponseUtil.ok(map);

    }

    @Override
    public Object findById(Integer id) {
        Map<String,Object> map=new HashMap<>();
        DtsOrder dtsOrder = dtsOrderMapper.findById(id);
        List<DtsGoods> goodsList=dtsGoodsService.findByOrderId(id);

        map.put("orderGoods",goodsList);
        map.put("order",dtsOrder);
        return map;
    }

    @Override
    public List<OrderStat> findOrderStat() {
        return dtsOrderMapper.findOrderStat();
    }
}
